/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.infosystem.noeplaza.lms.struts.action;

import com.eit.ntc.spring.utils.DateTimeUtils;
import com.eit.ntc.struts.action.BaseAjaxAction;
import com.eit.toschoolonline.lms.frontend.jdo.LearningItemHistoryM;
import com.eit.toschoolonline.lms.frontend.service.LearningItemHistoryService;
import com.eit.toschoolonline.lms.frontend.service.LmsViewService;
//import com.eit.toschoolonline.lms.service.LearningItemHistoryService;
//import com.eit.toschoolonline.lms.service.LmsServiceFactory;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Administrator
 */
public class CloseItemAction extends BaseAjaxAction{

    private LmsViewService lmsService;
    
    private LearningItemHistoryService historyService;
    
    @Override
    public String getXmlContent(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String xmlContent = "notFoundHistory";
        String refId = request.getParameter("refId");
        String sessionId = request.getSession().getId();
//        LearningItemHistoryService historyItem = (LearningItemHistoryService) historyService.getLearningItemHistory(refId, sessionId);
        LearningItemHistoryM history = historyService.getLearningItemHistory(refId,sessionId);
        if (history!=null){
            Date finishDate = DateTimeUtils.getEngNow();
            long usagetime =finishDate.getTime()- history.getAccessingDateTime().getTime();
            usagetime = usagetime/1000; // convert miliseconds to seconds
            history.setFinishDateTime(finishDate);
            history.setUsagetimeSeconds(usagetime);
            historyService.updateLearningItemHistory(history);
            xmlContent = "updateCompleted";
        };
        return xmlContent;
    }

    public LmsViewService getLmsService() {
        return lmsService;
    }

    public void setLmsService(LmsViewService lmsService) {
        this.lmsService = lmsService;
    }

    public LearningItemHistoryService getHistoryService() {
        return historyService;
    }

    public void setHistoryService(LearningItemHistoryService historyService) {
        this.historyService = historyService;
    }

   
}
